/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package edu.uts.reekie.designer.conceptual.edit;

import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;

import edu.uts.reekie.designer.Messages;
import edu.uts.reekie.designer.actions.ToggleMultiplicityAction;
import edu.uts.reekie.designer.conceptual.actions.SetUnsetComponentPersistenceStereotypeAction;
import edu.uts.reekie.designer.conceptual.actions.SetUnsetComponentPresentationStereotypeAction;
import edu.uts.reekie.designer.conceptual.actions.SetUnsetComponentRealTimeStereotypeAction;
import edu.uts.reekie.designer.conceptual.actions.SetUnsetComponentStereotypeAction;
import edu.uts.reekie.designer.conceptual.edit.*;
import edu.uts.reekie.designer.conceptual.model.ViewComponent;
import edu.uts.reekie.designer.conceptual.model.ViewComponentStereotype;
import edu.uts.reekie.designer.edit.*;
import edu.uts.reekie.designer.conceptual.model.*;
import edu.uts.reekie.designer.model.*;
import edu.uts.reekie.designer.model.commands.ToggleMultiplicityCommand;


public class ViewComponentEditPolicy
	extends ViewElementEditPolicy
{

public Command getCommand(Request request) {
	
	Object type = request.getType();
	if (SetUnsetComponentPersistenceStereotypeAction.getSetRequestName().equals(type)){
		return getSetUnsetStereotypeCommand(ViewComponentStereotype.PERSISTENCE,true);
	}
	if (SetUnsetComponentPersistenceStereotypeAction.getUnsetRequestName().equals(type)){
		return getSetUnsetStereotypeCommand(ViewComponentStereotype.PERSISTENCE,false);
	}
	
	if (SetUnsetComponentPresentationStereotypeAction.getSetRequestName().equals(type)){
		return getSetUnsetStereotypeCommand(ViewComponentStereotype.PRESENTATION,true);
	}
	if (SetUnsetComponentPresentationStereotypeAction.getUnsetRequestName().equals(type)){
		return getSetUnsetStereotypeCommand(ViewComponentStereotype.PRESENTATION,false);
	}
	
	if (SetUnsetComponentRealTimeStereotypeAction.getSetRequestName().equals(type)){
		return getSetUnsetStereotypeCommand(ViewComponentStereotype.REALTIME,true);
	}
	if (SetUnsetComponentRealTimeStereotypeAction.getUnsetRequestName().equals(type)){
		return getSetUnsetStereotypeCommand(ViewComponentStereotype.REALTIME,false);
	}
	
	if (ToggleMultiplicityAction.MULTIPLICITY.equals(type)){
		return getToggleMultiplicityCommand();
	}
	
	return super.getCommand(request);
}

protected Command getToggleMultiplicityCommand() {
	ToggleMultiplicityCommand cmd = new ToggleMultiplicityCommand();
	cmd.setTarget((Multiple)getHost().getModel());
	return cmd;
}

protected Command getSetUnsetStereotypeCommand(String stereotype, boolean set){
	SetUnsetStereotypeCommand cmd = new SetUnsetStereotypeCommand(stereotype, set);
	cmd.setChild((ViewComponent)getHost().getModel());
	return cmd;
}


static class SetUnsetStereotypeCommand extends org.eclipse.gef.commands.Command {
	
	private String stereotype;
	private boolean set;
	private ViewComponent child;
	
	public SetUnsetStereotypeCommand(String stereotype, boolean set) {
		super(set? "Enable ":"Disable " + stereotype);
		this.stereotype = stereotype;
		this.set = set;
	}
	
	public void setChild(ViewComponent vc){
		this.child = vc;
	}
	
	public void execute() {
		if (stereotype.equals(ViewComponentStereotype.PERSISTENCE)){
			child.setPersistence(set);
		} else if (stereotype.equals(ViewComponentStereotype.PRESENTATION)){
			child.setPresentation(set);
		} else if (stereotype.equals(ViewComponentStereotype.REALTIME)){
			child.setRealTime(set);
		}
		
	}
	
	public void undo() {
		set = !set;
		execute();
		set = !set;
	}
	
	public void redo() {
		execute();
	}
}

	
}
